﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SaintWinControl
{
    public partial class frmMatrix : Form
    {
        #region Property
        protected string baseUserControl = "SaintWinControl.ControlMatrix";
        protected List<ControlMatrix> ucControls = null;
        protected CRUDMode currentCRUDMode = CRUDMode.None;
        protected string focusControl = string.Empty;
        #endregion


        public frmMatrix()
        {
            InitializeComponent();
        }
        protected virtual void changeState()
        {
            switch ((int)currentCRUDMode)
            {
                case (int)CRUDMode.Insert:
                    insert();
                    break;
                case (int)CRUDMode.Exit:
                    exit();
                    break;
                case (int)CRUDMode.Cancel:
                    setAllControlDisable();
                    clearValue();
                    break;
                case (int)CRUDMode.Query:
                    query();
                    break;
                case (int)CRUDMode.InsertExecute:
                    insertExecute();
                    break;
                case (int)CRUDMode.Update:
                    update();
                    break;
                case (int)CRUDMode.UpdateExecute:
                    updateExecute();
                    break;
                case (int)CRUDMode.Delete:
                    deleteExecute();
                    break;
                case (int)CRUDMode.QueryExecute:
                    queryExecute();
                    break;
            }
        }

        protected virtual void exit()
        {
            this.Dispose();
        }


        #region CRUD
        /// <summary>
        /// 確定執行 Update 時
        /// </summary>
        protected virtual void updateExecute()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 當按下 Update 時要執行什麼
        /// </summary>
        protected virtual void update()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 當按下Insert 後要做什麼
        /// </summary>
        protected virtual void insert()
        {
            foreach (ControlMatrix cm in ucControls)
            {
                if (cm.AllowInsert.Equals(true))
                {
                    cm.Enabled = true;
                }
            }
        }

        /// <summary>
        /// 確定執行Insert
        /// </summary>
        protected virtual void insertExecute()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void query()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 確定執行Query 時
        /// </summary>
        protected virtual void queryExecute()
        {
            throw new NotImplementedException();
        }
        
        /// <summary>
        /// 確定執行 Delete 時
        /// </summary>
        protected virtual void deleteExecute()
        {
            throw new NotImplementedException();
        }
        #endregion

        /// <summary>
        /// 把自訂的控制項放到陣列中去
        /// </summary>
        protected virtual void fillControl()
        {
            ucControls = new List<ControlMatrix>();
            ControlMatrix cm = null;
            foreach (Control cnt in Controls)
            {
                if(isControlMatrix(cnt.GetType().BaseType.FullName))
                {
                    cm = cnt as ControlMatrix;
                    cm.Visible = cm.AllowVisible;
                    ucControls.Add(cm);
                }
            }
        }

        /// <summary>
        /// 只把ControlMatrix的物件放到集合中
        /// </summary>
        /// <param name="p_name"></param>
        /// <returns></returns>
        private bool isControlMatrix(string p_name)
        {
            return baseUserControl.Equals(p_name)
                ? true : false;
        }

        /// <summary>
        /// 把控制項中的textbox.Text 清空
        /// </summary>
        protected virtual void clearValue()
        {
            foreach (ControlMatrix cnt in ucControls)
            {
                cnt.ClearText();
            }
        }

        /// <summary>
        /// 把控制項設為Disable
        /// </summary>
        protected virtual void setAllControlDisable()
        {
            foreach (ControlMatrix cnt in ucControls)
            {
                cnt.Enabled = false;
            }
            //TODO：2012-03-02 未來再需要加入Gridview
        }

        protected virtual bool Validation()
        {
            return true;
        }

        public void ShowMessage(string p_msg)
        { 
            //TODO 應該要加入可以出現「是」、「否」、「取消」
            MessageBox.Show(p_msg,this.Text);
        }
    }
}
